﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

namespace ZhCAD
{
    public class FileTree
    {
        //public string Filename="";
        //public SortedDictionary<string, MLayer> Layers;
        //去掉以图层方式管理，方便以后的查询
        //public List<Feature> Entites;

        private readonly Dictionary<string, List<Feature>> _tree;//所有文件索引的图元
        private readonly Dictionary<string, MLayer> _layers;//所有图层

        public List<string> GetFiles()
        {
            List<string> files=new List<string>();
            foreach (string s in _tree.Keys)
            {
                files.Add(s);//Path.GetFileName
            }
            return files;
        }

        /// <summary>
        /// 添加图元
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="feature"></param>
        public void AddEnt(string filename, Feature feature)
        {
            if (!_tree.ContainsKey(filename))
            {
                _tree[filename]=new List<Feature>();
            }
            _tree[filename].Add(feature);
        }

        public Feature GetfeatureByHandle(ulong handle)
        {
            foreach (List<Feature> features in _tree.Values)
            {
                Feature feat = features.Find(x => x.Handle == handle);
                if (feat != null)
                    return feat;
            }
            return null;
        }
        public Feature GetfeatureById(int id)
        {
            foreach (List<Feature> features in _tree.Values)
            {
                Feature feat = features.Find(x => x.Id == id);
                if (feat != null)
                    return feat;
            }
            return null;
        }

        /// <summary>
        /// 添加图层
        /// </summary>
        /// <param name="layer"></param>
        public void AddLayer( MLayer layer)
        {
            if(!_layers.ContainsKey(layer.LayerName))
            _layers.Add(layer.LayerName,layer);
        }
        /// <summary>
        /// 获取指定文件的图层
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public List<string> GetLayers(string filename)
        {
            SortedSet<string> cSet = new SortedSet<string>();
            foreach (var pFeature in _tree[filename])
            {
                    cSet.Add(pFeature.Layer);
            }
            return cSet.ToList();
        }
        /// <summary>
        /// 获取指定图层
        /// </summary>
        /// <param name="layername"></param>
        /// <returns></returns>
        public MLayer GetLayer( string layername)
        {
            return _layers[layername];
        }
        /// <summary>
        /// 获取指定文件图元中的图元
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public List<Feature> GetFeaturesByLayer(string filename, string layer)
        {
            return (from pFeature in _tree[filename] where pFeature.Layer.Equals(layer, StringComparison.CurrentCultureIgnoreCase) select pFeature).ToList();
        }
        /// <summary>
        /// 获取指定文件图层下所有CASS码
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public List<string> GetCassByLayer(string filename, string layer)
        {
            SortedSet<string> cSet=new SortedSet<string>();
            foreach (var pFeature in _tree[filename])
            {
                if (pFeature.Layer.Equals(layer, StringComparison.CurrentCultureIgnoreCase))
                    cSet.Add(pFeature.Cass);
            }
            return cSet.ToList();
        }
        /// <summary>
        /// 获取指定文件图层下所有图元类型
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public List<string> GetShapeTypeByLayer(string filename, string layer)
        {
            SortedSet< string> cSet = new SortedSet<string>();
            foreach (var pFeature in _tree[filename])
            {
                if (pFeature.Layer.Equals(layer, StringComparison.CurrentCultureIgnoreCase))
                    cSet.Add(pFeature.EntType.ToString());
            }
            return cSet.ToList();
        }
        /// <summary>
        /// 根据CASS码返回所有图元id
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="layer"></param>
        /// <param name="cass"></param>
        /// <returns></returns>
        public List<int> GetIdsByCass(string filename, string layer, string cass)
        {
            return GetIdsByCass(GetFeaturesByLayer(filename, layer), cass);
        }
        /// <summary>
        /// 根据图元类型返回所有图元id
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="layer"></param>
        /// <param name="shapeType"></param>
        /// <returns></returns>
        public List<int> GetIdsByShapeType(string filename, string layer, string shapeType)
        {
            return GetIdsByShapeType(GetFeaturesByLayer(filename, layer), shapeType);
        }
        public static List<int> GetIdsByCass(List<Feature> list, string cass)
        {
            return (from pFeature in list where pFeature.Cass.Equals(cass, StringComparison.CurrentCultureIgnoreCase) select pFeature.Id).ToList();
        }
        public static List<int> GetIdsByShapeType(List<Feature> list, string shapeType)
        {
            return (from pFeature in list where pFeature.EntType.ToString().Equals(shapeType, StringComparison.CurrentCultureIgnoreCase) select pFeature.Id).ToList();
        }

        public FileTree()
        {
            _tree=new Dictionary<string, List<Feature>>();
            _layers=new Dictionary<string, MLayer>();
        }

        //public override string ToString()
        //{
        //    return string.IsNullOrEmpty( Filename)?"":Path.GetFileName(Filename);
        //}


    }

    public class MLayer
    {
        public Color Color;
        public List<Feature> Entites;
        public string LayerDesc;
        public string LayerName;
    }

    public class Feature
    {
        public string Layer;
        public string Cass;
        //public string CassDesc;
        public EntType EntType;
        public int Id;
        public ulong Handle;
  
    }

    public enum EntType
    {
        Point,
        Polyline,
        Polygon,
        Text
    }
}